<template>
  <div class="app-container">
    <el-dialog title="打印标签" :close-on-click-modal="false" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="选择打印机">
          <el-select v-model="form.printCode" clearable @change="chosePrinter">
            <el-option
              v-for="item in printerList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="总数量">
          <el-input v-model="form.number"  />
        </el-form-item>
        <el-form-item label="订单号码">
          <el-input type="textarea" v-model="form.orderNumber" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" icon="el-icon-printer" @click="printFirstContent">首尾页打印</el-button>
        <el-button type="primary" icon="el-icon-printer" @click="printContent">打印</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLodop } from '@/utils/LodopFuncs';
import { printOrder } from '@/api/order/order';
let LODOP;

export default {
  name: 'BatchPrintCode',
  data() {
    return {
      open: false,
      dataArray: [],
      form: {
        printCode: null,
        number: null,
        orderNumber: '',
      },
      printerList: [],
    }
  },
  methods: {
    openDialog(ids) {
      this.open = true;
      this.reset()
      this.getPrinters()
      printOrder(ids).then(res => {
        this.dataArray = res.data;
        var orderNumber = ''
        var number = 0
        for (let i = 0; i < this.dataArray.length; i++) {
          orderNumber += this.dataArray[i].code + ','
          number += this.dataArray[i].number
        }
       this.form.number = number
       this.form.orderNumber = orderNumber
      })
    },
    reset() {
      this.form = {
        printCode: null,
        number: null,
        orderNumber: '',
      }
    },
    getPrinters() {
      LODOP = getLodop()
      if (!LODOP) {
        this.$message.error('打印错误，请检查打印机是否安装正确！')
        return
      }
      this.printerList = []
      // 获取打印机个数
      let counter = LODOP.GET_PRINTER_COUNT()
      for (let i = 0; i < counter; i++) {
        this.printerList.push({
          // 将打印机存入printerList数组中
          name: LODOP.GET_PRINTER_NAME(i),
          value: i
        })
      }
      const printCode = this.form.printCode = this.$cache.local.get('print_label')
      if (printCode) {
        this.form.printCode = parseInt(printCode)
      }
    },
    chosePrinter(v) {
      // 将printCode 放入 key 为 print_label 的缓存
      if (v) {
        this.$cache.local.set('print_label', v)
      }
    },
    printContent() {
      // 获取LODOP对象的主过程
      LODOP = getLodop()
      if (!LODOP) {
        this.$message.error('打印错误，请检查打印机是否安装正确！')
        return
      }
      LODOP.PRINT_INITA(-1, -1, 315, 195, "标签打印");
      LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)

      for (let j = 0; j < this.dataArray.length; j++) {
        var datas = this.dataArray[j]
        var start = 1
        var totalNum = datas.number
        for (let i = start; i <= totalNum; i++) {
          LODOP.NewPage()
          LODOP.SET_PRINT_PAGESIZE(1, 800, 500, "B5");
          LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
          LODOP.ADD_PRINT_TEXT(9, 6, 85, 20, datas.time);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
          LODOP.ADD_PRINT_TEXT(8, 92, 51, 20, datas.transportPro);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
          LODOP.SET_PRINT_STYLEA(0, "TextFrame", 5);
          LODOP.ADD_PRINT_TEXT(30, 6, 80, 30, datas.startOrg);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
          LODOP.ADD_PRINT_TEXT(24, 84, 49, 34, "→");
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 25);
          LODOP.ADD_PRINT_TEXT(31, 130, 77, 30, datas.endOrg);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
          LODOP.ADD_PRINT_TEXT(7, 136, 142, 24, '单号:' + datas.code);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.ADD_PRINT_TEXT(103, 7, 188, 20, '收货人:' + datas.SHF.contactperson);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
          LODOP.SET_PRINT_STYLEA(0, "TextFrame", 2);
          LODOP.ADD_PRINT_TEXT(56, 5, 185, 20, datas.FHF.contactperson);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
          if (i === 1) {
            LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[0].productName + '-件数:共' + datas.number + '-第' + i);
          } else {
            LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[end - 1].productName + '-件数:共' + datas.number + '-第' + end);
          }
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.ADD_PRINT_TEXT(159, 8, 150, 30, "客户单号:" + datas.relateBill2);
          LODOP.ADD_PRINT_RECT(73, 6, 138, 22, 0, 1);
          if (i === 1) {
            LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[0].productCode);
          } else {
            LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[end - 1].productCode);
          }
          LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 2);
          LODOP.ADD_PRINT_TEXT(123, 7, 189, 36, "地址:" + datas.SHF.detaileAddress);
          LODOP.ADD_PRINT_TEXT(158, 156, 133, 31, "备注：" + datas.remark);
          LODOP.ADD_PRINT_TEXT(30, 207, 95, 40, datas.endZone);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        }
      }
      if (this.form.printCode && this.form.printCode !== 'null') {
        LODOP.SET_PRINTER_INDEXA(this.form.printCode)
      }
      LODOP.PRINT();
      // LODOP.PREVIEW()
      // LODOP.PRINT_DESIGN();
    },
    printFirstContent() {
      // 获取LODOP对象的主过程
      LODOP = getLodop()
      if (!LODOP) {
        this.$message.error('打印错误，请检查打印机是否安装正确！')
        return
      }

      LODOP.PRINT_INITA(-1, -1, 315, 195, "标签打印");
      LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)

      for (let j = 0; j < this.dataArray.length; j++) {
        var datas = this.dataArray[j]
        var start = 1
        var end = 1
        var totalNum = datas.number
        if (totalNum > 1) {
          end = 2
        }
        for (let i = start; i <= end; i++) {
          LODOP.NewPage()
          LODOP.SET_PRINT_PAGESIZE(1, 800, 500, "B5");
          LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
          LODOP.ADD_PRINT_TEXT(9, 6, 85, 20, datas.time);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
          LODOP.ADD_PRINT_TEXT(8, 92, 51, 20, datas.transportPro);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
          LODOP.SET_PRINT_STYLEA(0, "TextFrame", 5);
          LODOP.ADD_PRINT_TEXT(30, 6, 80, 30, datas.startOrg);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
          LODOP.ADD_PRINT_TEXT(24, 84, 49, 34, "→");
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 25);
          LODOP.ADD_PRINT_TEXT(31, 130, 77, 30, datas.endOrg);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
          LODOP.ADD_PRINT_TEXT(7, 136, 142, 24, '单号:' + datas.code);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.ADD_PRINT_TEXT(103, 7, 188, 20, '收货人:' + datas.SHF.contactperson);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
          LODOP.SET_PRINT_STYLEA(0, "TextFrame", 2);
          LODOP.ADD_PRINT_TEXT(56, 5, 185, 20, datas.FHF.contactperson);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
          if (i === 1) {
            LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[0].productName + '-件数:共' + datas.number + '-第' + i);
          } else {
            LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[end - 1].productName + '-件数:共' + datas.number + '-第' + end);
          }
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
          LODOP.ADD_PRINT_TEXT(159, 8, 150, 30, "客户单号:" + datas.relateBill2);
          LODOP.ADD_PRINT_RECT(73, 6, 138, 22, 0, 1);
          if (i === 1) {
            LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[0].productCode);
          } else {
            LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[end - 1].productCode);
          }
          LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 2);
          LODOP.ADD_PRINT_TEXT(123, 7, 189, 36, "地址:" + datas.SHF.detaileAddress);
          LODOP.ADD_PRINT_TEXT(158, 156, 133, 31, "备注：" + datas.remark);
          LODOP.ADD_PRINT_TEXT(30, 207, 95, 40, datas.endZone);
          LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
        }
      }
      if (this.form.printCode && this.form.printCode !== 'null') {
        LODOP.SET_PRINTER_INDEXA(this.form.printCode)
      }
      LODOP.PRINT();
      // LODOP.PREVIEW()
      // LODOP.PRINT_DESIGN();
    }
  }
}
</script>
